<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/8/27
 * Time: 17:25
 */

namespace app\mobile\controller;


use app\common\controller\ActionLogs;
use app\common\controller\Platurl;
use app\common\model\CardCzLog;
use app\common\model\CardGivecardLog;
use app\common\model\CardMember;
use app\common\model\CardShareUser;
use app\common\model\CardUserGet;
use app\common\model\Paylog;
use app\index\controller\Wxpay;
use think\Request;
use Wechat\Wxobjopen;

class Cardextend extends Mobilebase
{

    //获取开卡组件链接
    public function getcardusbizurl($param){

        $res = $this->cardusbizget($param);
        return $res;
    }


    //获取开卡组件链接循环 为解决第一次执行accesstoken miss 问题
    public function cardusbizget($param){
        $data['card_id'] = $param['card_id'];
        $data['outer_str'] = $param['outer_str'];
        $options = $this->options($param['card_id']);
        $weObj = new Wxobjopen($options);
        $res = $weObj->geturlActivate($data);
        if($res['errcode'] == 0){
            $qrcode_url = $res['url'];
            return $qrcode_url;
        }else{
            $this->cardusbizget($param);
        }
    }

    //分享二维码
    public function getshareqrcode(Request $request){
        $param = $request->param();

        $jg = action('Qrcodes/cardbizqrcode', array($param['id'], "https://" . $_SERVER['HTTP_HOST'] . "/mobile.php?s=/cardopen/share/share/" . $param['id'] . ".html"));
        $qr = $jg;
        $length = filesize($qr);
        $type = mime_content_type($qr);
        $showname = ltrim(strrchr($qr, '/'), '/');
        header("Content-Description: File Transfer");
        header('Content-type: ' . $type);
        header('Content-Length:' . $length);
        if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
            header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $showname . '"');
        }
        readfile($qr);
        exit;

    }

    //检查是否有充值送券并获取链接
    public function chackputurl($orderid){
        //   try {
        $Wxpay = new Wxpay();
        $Paylog = new Paylog();
        $CardCzLog = new CardCzLog();
        $CardGivecardLog = new CardGivecardLog();
        $CardUserGet = new CardUserGet();
        $id = $Paylog->where('orderid', $orderid)->field('mid,openid,price,orderid,cz_card_id,tzzt')->find();
        //获得订单信息END
        $updata['mid'] = $id['mid'];
        $updata['openid'] = $id['openid'];
        $updata['price'] = $id['price'];
        $updata['orderid'] = $id['orderid'];
        $updata['get_card_id'] = $id['cz_card_id'];
        if ((int)$id['tzzt'] == 0 || $id['tzzt'] == '0'|| $id['tzzt'] == "0") {
            $Wxpay->updata_card_infos($updata);
        }
        //调用充送END
        $give_rec_id = $CardCzLog->where('orderid', $updata['orderid'])->value('id');
        if ($give_rec_id) {
            $ress = $CardGivecardLog->where('give_rec_id', $give_rec_id)->find();
            if ($ress) {
                //查询领卡记录
                $user = $CardUserGet->where('id', $updata['get_card_id'])->field('code,card_id,id,nickname')->find();
                $Platurl = new Platurl();
                $agent_url = $Platurl->getagenturl('card_id', $user['card_id']);
                $url = $agent_url . '/mobile.php?s=/cardget/getcards/mid/' . $updata['mid'] . '/rec_id/' . $give_rec_id;
            } else {
                $url = 0;
            }
        }else{
            $url = 0;
        }
//        } catch (\Exception $e) {
//            $res = json_encode(array('code' => '9099', 'message' => $e->getMessage()));
//            $url = 0;
//        }
        //    $ActionLogs=new ActionLogs();
        //    $ActionLogs->writelog("payos","wxcard","put","e:",$url);
        return $url;
    }

}